{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "#class GradietntDescent:\n",
    "# a)\n",
    "\n",
    "def simple_activation(w, x , b):\n",
    "    return  w * x + b\n",
    "\n",
    "# b)\n",
    "def sigmoid_activation(w, x , b):\n",
    "    y = simple_activation(w,x,b)\n",
    "    return 1.0 / (1 + np.exp(-y))\n",
    "\n",
    "# derivative with respect with respect \"b\"        \n",
    "def derivative_bias(z, out):\n",
    "    return (z - out)\n",
    "\n",
    "# derivative with respect with respect \"w\"        \n",
    "def derivative_Weight(z, out , x):\n",
    "    return ((z - out) * x)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z : 9.984306091741224e-05\n",
      "b : 0.05004892309984951\n",
      "w : -0.04995107690015045\n",
      "iteration:  343\n"
     ]
    }
   ],
   "source": [
    "# a) wx + b\n",
    "#Initialization of Variables\n",
    "# Input\n",
    "x = 1\n",
    "# weight\n",
    "w = 0\n",
    "#bias\n",
    "b = 0.1\n",
    "# Output \n",
    "out = 0\n",
    "#learning rate\n",
    "alpha = 0.01\n",
    "\n",
    "\n",
    "iteration = 0\n",
    "z = 1\n",
    "#f(z)\n",
    "\n",
    "\n",
    "precision = 0.0001\n",
    "\n",
    "while abs(z - out ) > precision:\n",
    "    z = simple_activation(w, x , b)\n",
    "    #print(z)\n",
    "    iteration += 1\n",
    "    if z != out:\n",
    "        b = b - alpha * derivative_bias(z, out)\n",
    "        w = w - alpha * derivative_Weight(z, out , x)\n",
    "        \n",
    "\n",
    "\n",
    "print ('Z :', z)\n",
    "print( 'b :', b)\n",
    "print('w :' , w)\n",
    "print('iteration: ', iteration)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z : 0.00999962126281\n",
      "b : -2.24767904978\n",
      "w : -2.34767904978\n",
      "iteration:  5129\n"
     ]
    }
   ],
   "source": [
    "# a) wx + b\n",
    "\n",
    "#Initialization of Variables\n",
    "# Input\n",
    "#x = 1\n",
    "# weight\n",
    "#w = 0\n",
    "#bias\n",
    "#b = 0\n",
    "# Output \n",
    "#out = 0\n",
    "#learning rate\n",
    "#alpha = 0.1\n",
    "\n",
    "iteration = 0;\n",
    "y = 1\n",
    "precision = 0.01\n",
    "\n",
    "while abs(y - out ) > precision:\n",
    "    y = sigmoid_activation(w, x , b)\n",
    "    #print(y)\n",
    "    iteration += 1\n",
    "    if y != out:\n",
    "        b = b - alpha * derivative_bias(y, out)\n",
    "        w = w - alpha * derivative_Weight(y, out , x)\n",
    "        \n",
    "        \n",
    "print ('Z :', y)\n",
    "print( 'b :', b)\n",
    "print('w :' , w)\n",
    "print('iteration: ', iteration)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
